package com.example.jn_dxs.Ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;

import com.example.jn_dxs.Base.BaseActivity;
import com.example.jn_dxs.Base.BaseViewModel;
import com.example.jn_dxs.R;
import com.example.jn_dxs.Ui.MainActivity;
import com.example.jn_dxs.Utlis.SPCommon;
import com.example.jn_dxs.databinding.ActivityStartBinding;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class StartActivity extends BaseActivity<BaseViewModel, ActivityStartBinding> implements EasyPermissions.PermissionCallbacks {

    String[] needPermissions = {Manifest.permission.ACCESS_FINE_LOCATION,};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start;
    }

    @Override
    protected void initEventAndView() {
        if (EasyPermissions.hasPermissions(StartActivity.this, needPermissions)) {
            //已有权限
            binding.img.postDelayed(() -> {
                if (SPCommon.getInt("id", -1) == -1) {
                startActivity(LoginActivity.class);
//                    startActivity(MainActivity.class);

                } else {
                    startActivity(MainActivity.class);
                }
                finish();
            }, 3000);
        } else {

            // Ask for both permissions
            EasyPermissions.requestPermissions((Activity) this, "请开启定位", 100, needPermissions);
        }

    }

    @Override
    public void initClick() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (perms.size() == needPermissions.length)
            binding.img.postDelayed(() -> {
                if (SPCommon.getInt("id", -1) == -1) {
//                startActivity(LoginActivity.class);
                    startActivity(MainActivity.class);

                } else {
                    startActivity(MainActivity.class);
                }
                finish();
            }, 3000);
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

        if (EasyPermissions.somePermissionPermanentlyDenied(StartActivity.this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle("请确认权限是否开启")
                    .setPositiveButton("设置")
                    .setNegativeButton("取消")
                    .setRequestCode(101)
                    .build()
                    .show();
        }else {
            EasyPermissions.requestPermissions((Activity) this, "请开启定位", 100, needPermissions);
        }
    }
}
